## Summary Tables  =========================
n_responses <- function(x) {
  sum(!is.na(x))
}

#### Demographic Variable Summary Statistics #####
demographics <- c('pid3_w1', 'gender_w1', 'age4_w1', 'race4_w1', 'educ4_w1',  
                  'region_w1', 'socialclass_w1', 'income3_w1', 'sexuality3_recoded_w1', 
                  'marstat2_recoded_w1', 'socialclass3_recoded_w1',
                  'religimp2_recoded_w1', 'churatd3_recoded_w1', 'relig_recoded_w1', 
                  'religion_prolife_recoded_w1', 'personal_ideology_recoded_w1', 'pride_recoded_w1',
                  'usnative_recoded_w1', 'political_interest_recoded_w1', 'cable_cnn_w1',
                  'cable_msnbc_w1', 'cable_fox_w1')


#panel sample
sumtable(
  data_panel,
  out = "latex",
  vars = demographics,
  summ = c('notNA(x)','mean(x)','sd(x)','min(x)','max(x)'),
  title = "Panel Demographics (wave 1)",
  file = "clean_data/panel_demographics.html"
)

#cross-section demographics
sumtable(
  data_cross,
  out = "latex",
  vars = demographics,
  summ = c('notNA(x)','mean(x)','sd(x)','min(x)','max(x)'),
  title = "Cross Section Demographics (wave 1)",
  file = "clean_data/cross(w1)_demographics.html"
)



#### Dependent Variable Summary Statistics #####
cross_dvs <- c('aware_w1', 'treated_w1', 'info_source_sm_w1', 'info_source_cable_w1', 'info_source_local_w1',  
               'info_source_friend_w1', 'info_source_online_w1', 'info_source_newspaper_w1', 
               'info_source_email_w1', 'info_source_church_w1', 'behavior_w1', 'opinion_w1',
               'norm_w1', 'moral_opinion_w1', 'moral_norm_w1', 'personal_dobbs_w1', 'court_dobbs_w1',
               'personal_roe_w1', 'court_roe_w1', 'personal_rifle_w1', 'court_rifle_w1', 'personal_affirmative_w1',
               'court_affirmative_w1', 'importance_w1', 'fetal_scale_w1', 'approve_court_w1', 'courtlegit_1_w1',
               'courtlegit_2_w1', 'courtlegit_3_w1', 'courtlegit_4_w1', 'courtlegit_5_w1', 'courtlegit_recoded_w1', 
               'courtknow_composite_w1', 'courtideology_w1',
               'courtsize_w1', 'court_represent_w1', 'current_chief_w1', 'most_recent_w1', 'justice_selection_w1',
               'justice_tenure_w1', 'last_say_w1', 'republican_appointment_w1', 'courtterms_w1', 'political_interest_w1',
               'women_feelings_w1', 'dem_feelings_w1', 'rep_feelings_w1', 'affectivepolarization_w1',
               'parental_leave_w1')
sapply(data_cross, class)

data_cross[cross_dvs] <- sapply(data_cross[cross_dvs],as.numeric)
data_panel[cross_dvs] <- sapply(data_panel[cross_dvs],as.numeric)

#cross-section DVs
sumtable(
  data_cross,
  out = "latex",
  vars = cross_dvs,
  summ = c('notNA(x)','mean(x)','sd(x)','min(x)','max(x)'),
  title = "Cross Section DV's (wave 1)",
  file = "clean_data/cross(w1)_dvs"
)


panel_dvs <- c('personal_dobbs_w0', 'personal_dobbs_w1', 'court_dobbs_w0', 'court_dobbs_w1',
               'personal_roe_w0', 'personal_roe_w1', 'court_roe_w0', 'court_roe_w1', 
               'personal_rifle_w0', 'personal_rifle_w1', 'court_rifle_w0', 'court_rifle_w1', 
               'approve_court_w0', 'approve_court_w1', 'courtlegit_1_w0', 'courtlegit_1_w1', 
               'courtlegit_2_w0', 'courtlegit_2_w1', 'courtlegit_3_w0', 'courtlegit_3_w1', 
               'courtlegit_4_w0', 'courtlegit_4_w1', 'courtlegit_5_w0', 'courtlegit_5_w1',
               'courtlegit_composite_w0', 'courtlegit_composite_w1', 
               'justice_selection_w0', 'justice_selection_w1', 'justice_tenure_w0', 'justice_tenure_w1', 
               'last_say_w0', 'last_say_w1', 'republican_appointment_w0', 'republican_appointment_w1',
               'current_chief_w0', 'current_chief_w1', 'most_recent_w0', 'most_recent_w1',
               'courtknow_composite_w0', 'courtknow_composite_w1', 'courtsize_w0', 'courtsize_w1',
               'courtterms_w0', 'courtterms_w1')
data_panel[panel_dvs] <- sapply(data_panel[panel_dvs],as.numeric)

panel_dvs_cases <- c('personal_dobbs_w0', 'personal_dobbs_w1', 'court_dobbs_w0', 'court_dobbs_w1',
                     'personal_roe_w0', 'personal_roe_w1', 'court_roe_w0', 'court_roe_w1', 
                     'personal_rifle_w0', 'personal_rifle_w1', 'court_rifle_w0', 'court_rifle_w1')
panel_dvs_courtopinion <- c('approve_court_w0', 'approve_court_w1', 'courtlegit_1_w0', 'courtlegit_1_w1', 
                            'courtlegit_2_w0', 'courtlegit_2_w1', 'courtlegit_3_w0', 'courtlegit_3_w1', 
                            'courtlegit_4_w0', 'courtlegit_4_w1', 'courtlegit_5_w0', 'courtlegit_5_w1',
                            'courtlegit_composite_w0', 'courtlegit_composite_w1', 'courtsize_w0', 'courtsize_w1',
                            'courtterms_w0', 'courtterms_w1')
panel_dvs_courtknow <- c('justice_selection_w0', 'justice_selection_w1', 'justice_tenure_w0', 'justice_tenure_w1', 
                         'last_say_w0', 'last_say_w1', 'republican_appointment_w0', 'republican_appointment_w1',
                         'current_chief_w0', 'current_chief_w1', 'most_recent_w0', 'most_recent_w1',
                         'courtknow_composite_w0', 'courtknow_composite_w1')

#panel sample (with baselines)  
sumtable(
  data_panel,
  out = "latex",
  vars = panel_dvs_courtknow,
  summ = c('notNA(x)','mean(x)','sd(x)','min(x)','max(x)'),
  title = "Panel Vs - Court Knowledge",
  note = "Binary variables are coded such that 1 = yes, 2 = no"
)